﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool isStart = true;
        public MainWindow()
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.Topmost = true;
            InitializeComponent();
        }

        private void selectTime(object sender, MouseButtonEventArgs e)
        {

            MessageBox.Show(this.time.ToString());
        }

        public static List<Window1> _dialogs = new List<Window1>();

        private void Dialog_Closed(object sender, EventArgs e)
        {
            var closedDialog = sender as Window1;
            _dialogs.Remove(closedDialog);
        }
        double GetTopFrom()
        {
            //屏幕的高度-底部TaskBar的高度。
            double topFrom = System.Windows.SystemParameters.WorkArea.Bottom - 10;
            bool isContinueFind = _dialogs.Any(o => o.TopFrom == topFrom);
            while (isContinueFind)
            {
                topFrom = topFrom - 160;//此处100是NotifyWindow的高 160-100剩下的10  是通知之间的间距
                isContinueFind = _dialogs.Any(o => o.TopFrom == topFrom);
            }
            if (topFrom <= 0)
                topFrom = System.Windows.SystemParameters.WorkArea.Bottom - 10;
            return topFrom;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Window1 dialog = new Window1();//new 一个通知  
            dialog.msgh.Content = this.myMsg.Text;
            dialog.curTime.Content = this.message.Content;
            dialog.Closed += Dialog_Closed;
            dialog.TopFrom = GetTopFrom();
            dialog.Show();
            _dialogs.Add(dialog);
        }


        DispatcherTimer timer;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!isStart)
            {
                MessageBox.Show("您已启动成功，无需重复点击！");
                return;
            }

            string input = this.input1.Text;
            string myMsg = this.myMsg.Text;
            if (string.IsNullOrEmpty(myMsg))
            {
                MessageBox.Show("请您输入的要提醒的事情");
                return;
            }
            if (string.IsNullOrEmpty(input))
            {
                MessageBox.Show("请您输入的多久提醒一次");
                return;
            }
           
            Console.WriteLine($"您输入的是{input}");
            double it = 0;
            try {
                 it = double.Parse(input);
            } catch(Exception ex)
            {
                Console.WriteLine(ex);
                MessageBox.Show("您输入的间隔时间格式不合法，请输入整数，如：想在1小时后提醒请输入1");
                return;
            }
            timer = new DispatcherTimer();

            if (this.ss.IsChecked==true)
            {
                timer.Interval = TimeSpan.FromSeconds(it);
            }
            else
            {
                timer.Interval = TimeSpan.FromMinutes(it);
            }
            isStart = false;
            this.BtS.Background = new LinearGradientBrush(Colors.Gray, Colors.LightBlue, 20);
            MessageBox.Show("启动成功......");
            timer.Tick += timer1_Tick;
            timer.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.message.Content = $"当前时间：{DateTime.Now.ToString()}";//调用内容，并用lable1显示出来。。。
            Window1 dialog = new Window1();//new 一个通知  
            dialog.msgh.Content = this.myMsg.Text;
            dialog.curTime.Content = this.message.Content;
            dialog.Closed += Dialog_Closed;
            dialog.TopFrom = GetTopFrom();
            dialog.Show();
            _dialogs.Add(dialog);
        }

        private void input_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            this.ss.IsChecked = true;
        }

        private void mm_Checked(object sender, RoutedEventArgs e)
        {
            this.mm.IsChecked = true;
        }

        private void input_Initialized(object sender, EventArgs e)
        {
            this.mm.IsChecked = true;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
        }

        private void input1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
